package com.wudizaba.microservicesdemoauth.controller;


import com.wudizaba.microservicesdemoauth.module.dto.MicroServicesUserInfoDTO;
import com.wudizaba.microservicesdemoauth.module.dto.MicroservicesAuthRegisterDTO;
import com.wudizaba.microservicesdemoauth.module.po.MicroservicesUserinfo;
import com.wudizaba.microservicesdemoauth.service.IMicroservicesUserinfoService;
import com.wudizaba.microservicesdemocommon.R.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wudizaba
 * @since 2024-06-07
 */
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
@Tag(name = "登录接口")
public class MicroservicesUserinfoController {

    private final IMicroservicesUserinfoService microservicesUserinfoService;

    @Operation(summary = "登录")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true),
            @Parameter(name = "password", description = "密码", required = true)
    })
    @GetMapping("/login")
    public R<String> microservicesDemoAuthLogin(MicroServicesUserInfoDTO userinfo) {
        String result = microservicesUserinfoService.microservicesDemoAuthLogin(userinfo);
        return R.ok(result);
    }


    @Operation(summary = "退出")
    @PutMapping("/logout")
    public R<String> microservicesDemoAuthLogout() {
        String result = microservicesUserinfoService.microservicesDemoAuthLogout();
        return R.ok(result);
    }


    @Operation(summary = "获取用户信息")
    @GetMapping("/userinfo")
    public R<List<MicroservicesUserinfo>> microservicesDemoAuthUserinfo() {
        List<MicroservicesUserinfo> microservicesUserinfos = microservicesUserinfoService.microservicesDemoAuthUserinfo();
        return R.ok(microservicesUserinfos);
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public R<String> microservicesDemoAuthRegister(@RequestBody MicroservicesAuthRegisterDTO userinfo) {
        String result = microservicesUserinfoService.microservicesDemoAuthRegister(userinfo);
        return R.ok(result);
    }

    @Operation(summary = "注销")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true),
            @Parameter(name = "password", description = "密码", required = true)
    })
    @GetMapping("/cancel")
    public R<String> microservicesDemoAuthCancel(@NonNull MicroServicesUserInfoDTO userinfo) {
        String result = microservicesUserinfoService.microservicesDemoAuthCancel(userinfo);
        return R.ok(result);
    }
}
